package com.jww.service.impl;

import com.github.pagehelper.PageHelper;
import com.jww.domain.store.Catalog;
import com.jww.domain.store.Company;
import com.jww.mapper.store.CatalogMapper;
import com.jww.mapper.store.CompanyServiceMapper;
import com.jww.service.CompanyService;
import com.jww.utils.MapperUtil;
import com.jww.utils.MyUtils;
import com.jww.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {

    //StringUtils.isNotBlank();
    @Override
    public List<Company> findAll(String pageNum, String pageSize) {
        SqlSession sqlSession = null;

            sqlSession = MyUtils.getSqlSession();
            CompanyServiceMapper mapper = sqlSession.getMapper(CompanyServiceMapper.class);
            //分页
            //默认当前页为1
            int pageN = 1;
            //默认每页显示条目数为10
            int pageS = 10;
            //对获取的当前页码和每页显示条目数进行类型处理
            if (StringUtils.isNotBlank(pageNum)) {
                pageN = Integer.parseInt(pageNum);
            }
            if (StringUtils.isNotBlank(pageSize)) {
                pageS = Integer.parseInt(pageSize);
            }
            //设置分页参数：当前页码 每页显示的条目数
            PageHelper.startPage(pageN, pageS);

            List<Company> list = mapper.findAll();
            return list;


    }

    @Override
    public List<Company> findAll() {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MyUtils.getSqlSession();
            //2.获取Dao
            CompanyServiceMapper companyDao = sqlSession.getMapper(CompanyServiceMapper.class);
            //3.调用Dao层操作
            return companyDao.findAll();
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean deleteByIds(String ids) {
        SqlSession sqlSession = null;
        boolean flag = false;
        try {
            sqlSession = MyUtils.getSqlSession();
            CompanyServiceMapper mapper = sqlSession.getMapper(CompanyServiceMapper.class);

            String[] id = ids.split(",");
            mapper.delete(id);
            flag = true;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyUtils.close(sqlSession);
        }
        return flag;
    }

    //根据单个id查询
    @Override
    public Company findById(String id) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CompanyServiceMapper mapper = sqlSession.getMapper(CompanyServiceMapper.class);


        Company company = mapper.findById(id);
        MyUtils.close(sqlSession);
        return company;
    }

    @Override
    public void update(Company company) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CompanyServiceMapper mapper = sqlSession.getMapper(CompanyServiceMapper.class);


        mapper.update(company);
        MyUtils.close(sqlSession);
    }

    //新增
    @Override
    public void save(Company company) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CompanyServiceMapper mapper = sqlSession.getMapper(CompanyServiceMapper.class);

        //主键id处理
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        company.setId(id);

        mapper.save(company);
        MyUtils.close(sqlSession);

    }
    @Override
    public List<Company> list() {

        SqlSession session = MapperUtil.getSession();
        CompanyServiceMapper mapper = session.getMapper(CompanyServiceMapper.class);
        List<Company> list = mapper.findAll();
        MapperUtil.close(session);
        return list;

    }

}
